print(..., end=' ')
¶for i in range(4):
print(i)
0 1 2 3
for i in range(4):
print(i, end=' ')
0 1 2 3
for i in range(4):
print(i, end='**')
0**1**2**3**
for i in range(4):
print(i, end='\n')
0 1 2 3
numbers = [
[1, 2, 3],
[4, 5, 6]
]
numbers
[[1, 2, 3], [4, 5, 6]]
def print_grid(grid):
for row in grid:
for item in row:
print(item, end=' ')
print()
print_grid(numbers)
1 2 3 4 5 6
def empty_grid(num_rows, num_columns, value=None):
new_grid = []
for row in range(num_rows):
new_row = []
for column in range(num_columns):
new_row.append(value)
new_grid.append(new_row)
return new_grid
print_grid(empty_grid(4, 6))
None None None None None None None None None None None None None None None None None None None None None None None None
print_grid(empty_grid(4, 6, value='*'))
* * * * * * * * * * * * * * * * * * * * * * * *
def readgrid(filename):
with open(filename) as file:
lines = file.readlines()
grid = []
for line in lines:
grid.append(line.split())
return grid
grid = readgrid('grid_example.txt')
print_grid(grid)
. * . ! . * . * . ! . ! . * . * . ! . * .
grid = empty_grid(3, 5, value='.')
print_grid(grid)
print()
grid[0][4] = '*'
grid[1][2] = '?'
grid[2][1] = '!'
print_grid(grid)
. . . . . . . . . . . . . . . . . . . * . . ? . . . ! . . .
def has_value(grid, row, col, value):
if row < 0 or row >= len(grid):
return False
if col < 0 or col >= len(grid[row]):
return False
return grid[row][col] == value
print_grid(grid)
. . . . * . . ? . . . ! . . .
has_value(grid, 0, 0, '*')
False
has_value(grid, -1, 0, '*')
False
has_value(grid, 1, 2, '?')
True
Your homework assignment will be to finish the implementation of grid_game.py
.
The game logic is implemented for you. You just need to fill in a few functions that work with grids.
Have fun!